package com.alogic.nginx.xscript;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;


/**
 * 配置文件
 * @author yyduan
 * @since 1.6.12.25 [20190311 duanyy] <br>
 */
public class NginxConf extends Segment {

	/**
	 * 上下文id
	 */
	protected String cid = "$nginx-conf";
	
	/**
	 * 间隔
	 */
	protected String indent = "$nginx-indent";
	
	/**
	 * 版本信息
	 */
	protected String note = "#generated by alogic-ketty-nginx.";

	public NginxConf(String tag, Logiclet p) {
		super(tag, p);
		
		registerModule("ngx-conf",NginxConf.class);
		registerModule("ngx-section",NginxSection.class);
		registerModule("ngx-line",NginxLine.class);
		registerModule("ngx-save",NginxSave.class);
		registerModule("ngx-md5",NginxConentMd5.class);
		registerModule("ngx-server",NginxServer.class);
	}	
	
	@Override
	public void configure(Properties p){
		super.configure(p);
		
		cid = PropertiesConstants.getString(p, "cid", cid,true);
		note = PropertiesConstants.getString(p, "note", note,true);
		indent = PropertiesConstants.getString(p, "indent", indent,true);
	}
	
	@Override
	protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
		StringBuffer content = new StringBuffer(note);		
		try {
			ctx.setObject(cid, content);
			ctx.SetValue(indent, "0");
			super.onExecute(root, current, ctx, watcher);
		}finally{
			ctx.removeObject(cid);
		}
	}	
}
